﻿#define CONSOLE_DEBUG

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Jarvis.Server;
using Jarvis.Server.Domain;
using Jarvis.Server.IoC;
using Jarvis.Server.Persistence;
using Jarvis.Server.Workers;
using Ninject.Modules;
using Topshelf;
using Ninject;



namespace JarvisServer
{
    class Program
    {
        static void Main(string[] args)
        {
#if CONSOLE_DEBUG
            Console.WriteLine("Running in debug mode:Delete db (y/n)?");
            while (true)
            {
                char pressed = Console.ReadKey().KeyChar;
                if(pressed == 'y')
                {
                    File.Delete("jarvis.sqlite3");
                    break;
                }
                else if(pressed == 'n')
                {
                    break;
                }
            }
            
            Console.WriteLine("\n");
#endif
            using(var kernel = CreateKernel()){
                HostFactory.Run(c =>
                                    {
                                        c.Service<JarvisWindowsService>(() => kernel.Get<JarvisWindowsService>());
                                        c.RunAsLocalSystem();
                                        c.SetDisplayName("Jarvis");
                                        c.SetServiceName("Jarvis");
                                        c.SetDescription("Jarvis: your personal butler");
                                    });
            }


        }

        static IKernel CreateKernel()
        {

            var modules = new INinjectModule[]
                                           {
                                               new NhibernateModule(),
                                               new JarvisModule(), 
                                               new AutoMapperModule(), 
                                           };

           var kernel = new StandardKernel(modules);

           kernel.Bind<JarvisWindowsService>().ToSelf();
           kernel.Bind<LocatorWorker>().ToSelf();

           return kernel;
        }

        
    }
}
